<?php

namespace app\api\controller;

class ApiBase  {

	/**
	 * 获取提交的参数数据
	 * @var type
	 */
	private $paramData = null;

	/**
	 * 提交的签名值
	 * @var type
	 */
	private $signValue = '';

	/**
	 * 加密key
	 * @var type
	 */
	protected $apikey = '7E7FC0F87CA3FE970CD901754A935136';
	protected $sign_passtime = 86400;


	protected $base_url;

	protected function _initialize() {
		header('Access-Control-Allow-Origin:*');
		$this->base_url = config('base_url');
		if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
			return true;
		}
		//签名验证
		if (!empty($_POST)) {
			$this->signValue = $this->_post('sign'); //取出签名
			$this->paramData = $this->getPostData();
			$this->checkSign();
		} else if (!empty($_GET)) {
			
		}
	}

	protected function _post($name, $default = '', $filter = '') {
        return request()->param($name, $default, $filter);
    }
    
	/**
	 *
	 * 检测签名
	 */
	protected function checkSign() {
		if (!isset($this->paramData['t'])) {
			$this->failJSON(lang('API_WRONG_SIGN'));
		}
		if ((time() - $this->paramData['t']) > $this->sign_passtime) {
			$this->failJSON(lang('API_TIMEOUT'));
		}
		$sign = $this->toUrlParams();
		if ($this->signValue === $sign) {
			return true;
		} else {
			$this->failJSON(lang('API_WRONG_SIGN'));
		}
	}

	/**
	 * 生成签名
	 */
	private function makeSign() {
		return strtoupper(md5($this->paramData['t'] . $this->apikey));
	}

	/**
	 * 格式化参数格式化成url参数(字典排序后重组)
	 */
	private function toUrlParams() {
		$buff = '';
		foreach ($this->paramData as $k => $v) {
			if ($v != '' && !is_array($v)) {
				if (preg_match('/[\x80-\xff]./', $v))
					$buff .= $k . '=' . urldecode($v) . '&';
				else
					$buff .= $k . '=' . $v . '&';
			}
		}
		$buff = trim($buff, '&');
		return strtoupper(md5($buff));
	}

	

	/**
	 * 获取POST提交的数据。
	 * @return type
	 */
	protected function getPostData() {
		$data = array();
		if (isset($_POST)) {
			$post_data = $_POST;
			$data = !empty($post_data) ? $post_data : array();
			unset($data['sign']);
			$data['api_key'] = $this->apikey;
		} else {
			return $this->failJSON('post data is empty');
		}
		return $data;
	}

	/**
	 * 输出错误JSON信息。
	 * @param type $message
	 */
	protected function failJSON($message) {
		$message = lang($message);
		$jsonData = array('success' => false, 'message' => $message);
		$json = json_encode($jsonData, true);
		echo $json;
		exit;
	}

	/**
	 * 输出成功JSON信息
	 * @param type $data
	 */
	protected function successJSON($data = NULL, $msg = "success") {
		$jsonData = array('success' => true, 'data' => $data, 'message' => $msg);
		$json = json_encode($jsonData, 1);
		echo $json;
		exit;
	}

	/**
	 * 获取当前页码
	 * @return type
	 */
	protected function getPageIndex() {
		$pageIndex = $this->_get('page');
		if (empty($pageIndex))
			$pageIndex = -1;
		return $pageIndex;
	}

	/**
	 * 获取每页显示数量
	 * @return type
	 */
	protected function getPageSize() {
		$pageSize = $this->_get('rows');
		if (empty($pageSize))
			$pageSize = -1;
		if ($pageSize > 50)
			$pageSize = 50;
		return $pageSize;
	}

	public function _empty() {
		echo "empty";
	}


	protected function getSize($filesize) {
        if ($filesize >= 1099511627776) {
            $filesize = round($filesize / 1099511627776 * 100) / 100 . ' TB';
        } elseif ($filesize >= 1073741824) {
            $filesize = round($filesize / 1073741824 * 100) / 100 . ' GB';
        } elseif ($filesize >= 1048576) {
            $filesize = round($filesize / 1048576 * 100) / 100 . ' MB';
        } elseif ($filesize >= 1024) {
            $filesize = round($filesize / 1024 * 100) / 100 . ' KB';
        } else {
            $filesize = $filesize . ' B';
        }
        return $filesize;
    }

}
